<?php
/*
 * 分级的扩展
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: d2ddddea4a6729c823e4f452feeda7e3aed9c399 $
 */

/**
 * 分类的扩展
 * Class AutoRank
 */
class AutoRank {

	const LEVEL_BRAND    = 0;
	const LEVEL_MANUFACT = 1;
	const LEVEL_CAR      = 2;

	/**
	 * 根据级别信息获取需要处理的扩展数据表的名称
	 * @param $rankInfo
	 * @return string
	 */
	public static function table($rankInfo) {
		if ($rankInfo['typename'] != AutoCategory::TYPE_RANK) return '';
		return self::tableByLevel($rankInfo['level']);
	}

	/**
	 * 根据级别获取需要处理的表的名称
	 * @param $level
	 * @return string
	 */
	public static function tableByLevel($level) {
		if ($level == self::LEVEL_BRAND) {
			return Table::name(Table::NAME_AUTO_RANK_BRAND);
		} else if ($level == self::LEVEL_MANUFACT) {
			return Table::name(Table::NAME_AUTO_RANK_MANUFACT);
		} else if ($level == self::LEVEL_CAR) {
			return Table::name(Table::NAME_AUTO_RANK_CAR);
		}
		return '';
	}

	/**
	 * 获取缓存的表字段
	 * @param $table
	 * @return array|mixed|string
	 */
	public static function fields($table) {
		global $db;
		return PCache::read('fields-' . str_replace($db->getPre(), '', $table) . '.php');
	}


	/**
	 * 获取所有品牌
	 */
	public static function getBrands($fields = array(), $adfts = array()) {
		$RANK   = AutoCategory::getCache(AutoCategory::TYPE_RANK);
		$brands = array();

		foreach ($RANK as $rank) {
			if ($rank['level'] == 0) {
				$rank['letter'] = strtoupper($rank['letter']);
				$rank['name']   = $rank['letter'] . '-' . $rank['name'];
				$arr            = array();
				if (!empty($fields)) {
					foreach ($fields as $key => $field) {
						$arr[$field] = $rank[$key];
					}
					$arr['letter'] = $rank['letter'];
				} else {
					$arr = $rank;
				}

				if (in_array($rank['arid'], $adfts)) {
					$arr['selected'] = 'true';
				}
				$brands[] = $arr;
			}
		}
		usort($brands, 'xd_catsort');
		return $brands;
	}


	/**
	 * 获取所有车款
	 */
	public static function getCars($fields = array(), $parents = array(), $adfts = array()) {
		global $db;
		$table = Table::name(Table::NAME_AUTO_CATEGORY);
		$RANK  = $db->getAll("select * from {$table} where typename='rank' and level=2 order by listorder asc");
		$cars  = array();

		foreach ($RANK as $rank) {
			// 层级修复有问题, 跳过
			$arrparentid  = $rank['arrparentid'];
			$parentids    = explode(',', $arrparentid);
			$manufactorid = $parentids[2] ? intval($parentids[2]) : 0;
			$brandid      = $parentids[1] ? intval($parentids[1]) : 0;
			if (!$manufactorid) continue;
			if (in_array($brandid, $parents)) {
				$arr = array();
				if (!empty($fields)) {
					foreach ($fields as $key => $field) {
						$arr[$field] = $rank[$key];
					}
				} else {
					$arr = $rank;
				}
				if (in_array($rank['arid'], $adfts)) {
					$arr['selected'] = 'true';
				}
				$cars[] = $arr;
			}
		}
		return $cars;
	}

	/**
	 * 获取所有相关车款
	 * @param array  $fields
	 * @param string $acids
	 * @return array
	 */
	public static function getModels($fields = array(), $acids = '') {
		global $db;
		$table   = Table::name(Table::NAME_AUTO_MODEL);
		$vfields = array_keys($fields);
		if (!$vfields) $vfields = '*';
		$sql    = "select " . implode(',', $vfields) . " from {$table} where arcid in ($acids) order by listorder asc";
		$MODEL  = $db->getAll($sql);
		$models = array();

		foreach ($MODEL as $model) {
			$arr = array();
			if (!empty($fields)) {
				foreach ($fields as $key => $field) {
					$arr[$field] = $model[$key];
				}
			} else {
				$arr = $model;
			}
			$models[] = $arr;
		}
		return $models;
	}


	/**
	 * 车系id 搞到 生产商ID
	 */
	public static function getArmidByArcid($arcid) {
		global $db;
		$table = Table::name(Table::NAME_AUTO_CATEGORY);
		return $db->getVar("select parentid from {$table} where arid={$arcid}", 'parentid');
	}

	/**
	 * 从车系ID 搞到 品牌ID
	 * @param $arcid
	 * @return mixed
	 */
	public static function getArbidByArcid($arcid) {
		$armid = self::getArmidByArcid($arcid);
		$RANK  = AutoCategory::getCache(AutoCategory::TYPE_RANK);
		return $RANK[$armid]['parentid'];
	}

	public static function getArbidByArmid($armid) {
		$RANK = AutoCategory::getCache(AutoCategory::TYPE_RANK);
		return $RANK[$armid]['parentid'];
	}
	/**
	 * @param $level
	 * @param $arid
	 * @return array|null
	 */
	public static function get($level, $arid) {
		global $db;
		$table = Table::name(Table::NAME_AUTO_CATEGORY);
		$tableExt = self::tableByLevel($level);
		$sql = "select t.*, te.* from {$table} t, {$tableExt} te where t.arid = te.arid and t.arid = {$arid}";
		return $db->getOne($sql);
	}

}